package com.smartshopping.iu;

import com.smartshopping.model.DataBaseHelper;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.widget.*;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

public class IndexActivity extends Activity {

	@Override
	// EFFECTS: khoi tao gia tri can ban su dung trong layout Giao Dien Chinh
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		//Startup screen start
		setContentView(R.layout.activity_starting);
	    new AsyncTask<Object, Object, Object>() {
	        protected Object doInBackground(Object... params) {
	            try {
	                // If you have nothing to do just sleep
	                Thread.sleep(1000);
	            } catch (InterruptedException e) {
	                // handle this
	            }
	            return (Object)null;
	        }

	        protected void onPostExecute(Object result) {
	            // This runs on UI thread
	        	setContentView(R.layout.activity_index);
	        	adjustDisplay();
	    		handlerAction();
	        }
	    }.execute((Object) null);
	    
	    // Khoi tao database luc bat dau chay chuong trinh
	    // Su dung trong giai doan phat trien vi database chua hoan thien
	    // se duoc xoa khi database hoan thien
	    DataBaseHelper myDbHelper = new DataBaseHelper(this);
//	    myDbHelper.delDatabase();
	}

// <<------------------------------------------------------------------>>

	//  Adjust the button in screen
	public void adjustDisplay()
	{
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int x = metrics.widthPixels / 4;
		int y = (metrics.heightPixels / 2) / 6;
		
		// Classic button
		ImageView imageView_1 = (ImageView)findViewById(R.id.index_button_imageview1);
		LayoutParams params = (LayoutParams) imageView_1.getLayoutParams();
		params.height = y;
		imageView_1.setLayoutParams(params);
		
		// Advanced button
		ImageView imageView_2 = (ImageView)findViewById(R.id.index_button_imageview2);
		params = (LayoutParams) imageView_2.getLayoutParams();
		params.height = y;
		imageView_2.setLayoutParams(params);
		
		// News button
		ImageView imageView_3 = (ImageView)findViewById(R.id.index_button_imageview3);
		params = (LayoutParams) imageView_3.getLayoutParams();
		params.height = y;
		imageView_3.setLayoutParams(params);
		
		// Notes button
		ImageView imageView_4 = (ImageView)findViewById(R.id.index_button_imageview4);
		params = (LayoutParams) imageView_4.getLayoutParams();
		params.height = y;
		imageView_4.setLayoutParams(params);
		
		// Exit button
		ImageView imageView_5 = (ImageView)findViewById(R.id.index_button_imageview5);
		params = (LayoutParams) imageView_5.getLayoutParams();
		params.height = y;
		imageView_5.setLayoutParams(params);
		
		// About button
		ImageView imageView_6 = (ImageView)findViewById(R.id.index_button_imageview6);
		params = (LayoutParams) imageView_6.getLayoutParams();
		params.height = y;
		imageView_6.setLayoutParams(params);
		
	}

// <<------------------------------------------------------------------>>	

	// Call suitable function when user click on the button on layout "Home page"
	public void handlerAction()
	{
		// Here is code to go grab and layout the Buttons, they're named b1, b2, etc. and identified as such. 
		ImageView imageView_1 = (ImageView)findViewById(R.id.index_button_imageview1);
		ImageView imageView_2 = (ImageView)findViewById(R.id.index_button_imageview2);
		ImageView imageView_3 = (ImageView)findViewById(R.id.index_button_imageview3);
		ImageView imageView_4 = (ImageView)findViewById(R.id.index_button_imageview4);
		ImageView imageView_5 = (ImageView)findViewById(R.id.index_button_imageview5);
		ImageView imageView_6 = (ImageView)findViewById(R.id.index_button_imageview6);
				
		setOnClick(imageView_1);
		setOnClick(imageView_2);
		setOnClick(imageView_3);
		setOnClick(imageView_4);
		setOnClick(imageView_5);
		setOnClick(imageView_6);
	}

// <<------------------------------------------------------------------>>
	
//  Set the handler of each imageButton	OnClick
	public void setOnClick(ImageView img) {
		img.setOnClickListener(new OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	// Kiem tra ID cua button goi layout phu hop
	        	switch(v.getId()) { 
			    	case R.id.index_button_imageview1:   
			    		Intent myIntent1 = new Intent(IndexActivity.this, MarketActivity.class);
			    		IndexActivity.this.startActivity(myIntent1);
			    	break;
			    		 
			    	case R.id.index_button_imageview2:
			    		Intent myIntent2 = new Intent(IndexActivity.this, AdvanceActivity.class);
			    		IndexActivity.this.startActivity(myIntent2);
			    	break;
			    	
			    	case R.id.index_button_imageview3:
			    		Intent myIntent3 = new Intent(IndexActivity.this, ListNewsActivity.class);
			    		IndexActivity.this.startActivity(myIntent3);
			    	break;
			    	
			    	case R.id.index_button_imageview4:
			    		Intent myIntent4 = new Intent(IndexActivity.this, ListBillActivity.class);
			    		IndexActivity.this.startActivity(myIntent4);
			    	break;
			    	
			    	case R.id.index_button_imageview5:
			    		finish();
			            System.exit(0);
			    	break;
			    	
			    	case R.id.index_button_imageview6:
			    		Intent myIntent6 = new Intent(IndexActivity.this, AboutActivity.class);
			    		IndexActivity.this.startActivity(myIntent6);
			    	break;
	        	} 
	        }
	    });
	}
	
// <<------------------------------------------------------------------>>	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.index, menu);
		return true;
	}

}
